コンピュータサイエンスにおいて、 ポインター は基本的な形の 間接参照です。値を直接保持するのではなく、ポインター変数は メモリアドレス——その値が格納されている具体的な場所——を保持します。これにより、プログラムはデータの重複コピーを避けながら、一元的な真理への変更を調整できます。
1. アドレスの論理
値が格納される場所は、その メモリアドレスとして知られています。これを理解することは、コンピュータの内部言語を話す第一歩です。Goでは、アンド(&)を使ってアドレスを取得し、アスタリスク(*)を使ってアドレスをたどります。&) でアドレスを見つけ、アスタリスク(*) でアドレスをたどります。
2. なぜ間接参照が重要なのか
間接参照は、複雑で共有されるデータ構造を構築する強力なツールです。新しい住所へと案内する店舗の看板を想像してください。看板にはお店自体は含まれていません。それはあなたに どこ を見に行く場所を教えてくれます。Goはこのスキルを習得する安全な環境を提供しています。もし以前にポインターに触れたことがあるなら、深呼吸してください。そんなに悪いものではありません。初めての経験なら、リラックスしておいてください。Goはポインターを学ぶのに安全な場所です。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Like the shop sign directing visitors to a new address, pointers direct a computer where to look for a value. What's another situation where you're directed to look somewhere else?
A URL redirecting to a new website.
Reading the final page of a book directly.
Calculating 2 + 2 in your head.
A static variable that never changes.
✅ Correct!
Correct! URLs, library index cards, and table of contents are all real-world forms of indirection.❌ Incorrect
Think about things that act as references to other locations rather than the content itself.QUESTION 2
How do you know that
time.Time never uses a pointer receiver?It is an immutable type that returns a new value for every method call.
It is a primitive type like int.
The Go compiler prohibits pointers on time structs.
It uses global variables instead.
✅ Correct!
Exactly. Methods like .Add return a new time.Time instead of modifying the existing one, which is characteristic of value receivers.❌ Incorrect
Check the documentation: methods return a new instance of Time rather than mutating the current one.QUESTION 3
What is the result of using the address operator (
&) on a variable?It returns the value stored in the variable.
It returns the memory address (location) of the variable.
It doubles the memory usage of the variable.
It deletes the variable from memory.
✅ Correct!
The ampersand is the 'address-of' operator.❌ Incorrect
The address operator finds the 'where', not the 'what'.QUESTION 4
Quick check 26.1: If a pointer is a signpost, what happens if the building it points to is painted a different color? Does the signpost need to change?
Yes, because the signpost must describe the color.
No, because the address (location) remains the same.
Yes, the pointer automatically breaks.
No, pointers only point to grayscale buildings.
✅ Correct!
Correct. Changing the data at the address doesn't change the address itself.❌ Incorrect
The pointer only cares about the location, not the internal state of the data stored there.QUESTION 5
Which statement best describes Go's approach to pointers?
It allows dangerous pointer arithmetic like C.
It is a safe environment that handles many complexities for you.
Pointers are not allowed in Go.
Pointers only work with integers.
✅ Correct!
Go is designed to be a safe place to learn and use pointers without many of the pitfalls of lower-level languages.❌ Incorrect
Review the 'Tips' section: Go is a safe place to learn pointers.Module 7 Challenge: Indirection and Sudoku Logic
Applying Pointers to Shared State Management
You are tasked with building a Sudoku validation engine. In Sudoku, the grid must be shared across various validation rules (row, column, subgrid). Passing the entire 81-element array by value would be inefficient. You must use pointer receivers to mutate the grid and handle errors gracefully.
Q
1. Write a line of code to sort a slice named 'food' from the shortest to longest string using 'sort.Slice' and a closure. [Word Count Requirement: 15 words]
Solution:
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
Q
2. If an error occurred while writing 'Clear is better than clever.' to a file in Listing 28.6, what sequence of events would follow?
Solution:
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
Q
3. Implement a method signature to clear a digit from a square in a Sudoku struct. This method need not adhere to constraints, as several squares may be empty (zero).
Solution:
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }